import threading
import queue
import time


class MessageQueue:
    def __init__(self):
        self.queue = queue.Queue()

    def produce(self, message):
        self.queue.put(message)

    def consume(self):
        while True:
            message = self.queue.get()
            if self.queue.empty():
                break
            print(message)
            time.sleep(1)


def producer(queue_instance):
    for i in range(5):
        message = f"Message {i}"
        queue_instance.produce(message)
        print(f"Produced: {message}")
        time.sleep(0.5)


if __name__ == '__main__':
    message_queue = MessageQueue()
    # 创建上缠着
    producer_thread = threading.Thread(target=producer, args=(message_queue,))

    # 创建消费者
    consumer_thread = threading.Thread(target=message_queue.consume())

    # 启动线程
    producer_thread.start()
    consumer_thread.start()

    # 等待生产者线程和消费者线程结束
    producer_thread.join()
    consumer_thread.join()
